package com.cskaoyan.controller;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.bean.params.RoleCreateBO;
import com.cskaoyan.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @USER: cuiweiguo#
 * @DATE: 2022/5/6
 * @TIME: 11:00
 * @PROJECT_NAME: mallproject2
 */
@RestController
@RequestMapping("admin/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    @RequestMapping("list")
    public AdminListRespVo<RoleListBean> list(String name, BaseParam baseParam) {
        //GET http://182.92.235.201:8083/admin/role/list?page=1&limit=20&sort=add_time&order=desc
        //数据库查询
        RoleListBean roleListBean = roleService.list(name, baseParam);
        return AdminListRespVo.listOk(roleListBean);
    }

    @RequestMapping("options")
    public AdminListRespVo<LabelListBean> options() {
        //GET http://182.92.235.201:8083/admin/role/list?page=1&limit=20&sort=add_time&order=desc
        //TODO：数据库查询----这里可能需要修改--新建了表label，其实可以不加表
        RoleListBean labelListBean = roleService.options();
        return AdminListRespVo.listOk(labelListBean);
    }

    @PostMapping("create")
    public AdminListRespVo<LabelListBean> create(@RequestBody RoleCreateBO roleCreateBO) {
        //POST http://182.92.235.201:8083/admin/role/create HTTP/1.1
        //业务1；role数据表插入数据 业务2：label数据表插入数据
        RoleCreateVO roleCreateVO = roleService.create(roleCreateBO);
        return AdminListRespVo.listOk(roleCreateVO);
    }

    @PostMapping("delete")
    public AdminListRespVo<String> delete(@RequestBody Role role) {
        //POST http://182.92.235.201:8083/admin/role/create HTTP/1.1
        //业务1；role数据表插入数据 业务2：label数据表插入数据
        int code = roleService.delete(role);
        if (code == 200) {
            return AdminListRespVo.listOk("null");
        }
        return null;
    }

    @PostMapping("update")
    public AdminListRespVo<String> update(@RequestBody Role role) {
        //POST http://182.92.235.201:8083/admin/role/create HTTP/1.1
        //业务1；role数据表插入数据 业务2：label数据表插入数据
        int code = roleService.update(role);
        if (code == 200) {
            return AdminListRespVo.listOk("null");
        }
        return null;
    }

    @GetMapping("permissions")
    public AdminListRespVo<RolePermissions> getPermissions(Integer roleId) {
        //GET http://182.92.235.201:8083/admin/role/permissions?roleId=53 HTTP/1.1
        RolePermissions rolePermissions = roleService.getPermissionsByRoleId(roleId);
        return AdminListRespVo.listOk(rolePermissions);
    }

    /**
     * @Description:
     * @Author: cuiweiguo#
     * @Date: 2022/5/9 23:09
     * @Param: [roleId, permissions]:
     * @return: com.cskaoyan.bean.AdminListRespVo<com.cskaoyan.bean.RolePermissions>:
     **/
    @PostMapping("permissions")
    public AdminListRespVo<RolePermissions> postPermissions(@RequestBody PostPermissions postPermissions) {
        int code = roleService.postPermissions(postPermissions.getRoleId(), postPermissions.getPermissions());

        return AdminListRespVo.listOk("null");
    }
}
